﻿using System;
using System.Collections.Generic;

namespace Mersiv.App.Client.Logging
{

  /// <summary>
  /// Potential candidate for a Singleton.
  /// </summary>
  internal sealed class LogManager
  {

    private static volatile LogManager instance;
    private static object syncRoot = new Object();

    private List<LogMessage> logMessageList;

    private LogManager()
    {
      this.logMessageList = new List<LogMessage>();
    }

    public static LogManager Instance
    {
      get
      {
        if (LogManager.instance == null)
        {
          lock (LogManager.syncRoot)
          {
            if (LogManager.instance == null)
            {
              LogManager.instance = new LogManager();
            }
          }
        }
        return LogManager.instance;
      }
    }


    public void Add(string message)
    {
      this.Add(new LogMessage(message));
    }
    public void Add(LogMessage logMessage)
    {
      this.logMessageList.Add(logMessage);
      this.OnLogMessageAdded(logMessage);
    }

    public event LogEvent LogMessageAdded;
    private void OnLogMessageAdded(LogMessage logMessage)
    {
      if (this.LogMessageAdded != null)
      {
        this.LogMessageAdded.Invoke(logMessage);
      }
    }

  }

}